﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCodeSolutions
{
    /// <summary>
    /// 35. 搜索插入位置
    /// </summary>
    class LeetCode35: LeetCodeBase
    {
        public override void Execute()
        {
            CommonFuntions.Print(SearchInsert(new int[] { 1 }, 2));
        }

        public int SearchInsert(int[] nums, int target)
        {
            if (nums == null || target < nums[0])
            {
                return 0;
            }

            if (nums.Length == 1)
            {
                return nums[0] == target ? 0 : 1;
            }

            if (target > nums[nums.Length - 1])
            {
                return nums.Length;
            }

            for (int i = 0; i < nums.Length - 1; i++)
            {
                CommonFuntions.Print(string.Format("nums{0} {1} : {2}", i, nums[i], target));
                if (nums[i] == target)
                {
                    return i;
                }
                else if (nums[i] < target && target < nums[i + 1])
                {
                    return i + 1;
                }
                else if (nums[i + 1] == target)
                {
                    return i + 1;
                }
            }

            CommonFuntions.Print("Final");
            return -1;
        }
    }
}
